package com.cmpan.networkdisk.controller;

import com.cmpan.networkdisk.model.Disk;
import com.cmpan.networkdisk.model.Page;
import com.cmpan.networkdisk.model.Type;
import com.cmpan.networkdisk.model.User;
import com.cmpan.networkdisk.service.DiskService;
import com.cmpan.networkdisk.service.TypeService;
import com.cmpan.networkdisk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
 * ClassName:UserController
 * Package:com.cmpan.networkdisk.controller
 * Description:
 *
 * @Date:2020/7/5 17:04
 * @com.chuangmei
 */
@Controller
@RequestMapping("/user")
public class LoginController {

    //service查询用户名
    @Autowired
    private UserService userService;

    @Autowired
    private DiskService diskService;

    @Autowired
    private TypeService typeService;


    //登录,此请求不能使用拦截器拦截
    @PostMapping("/login")
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public String login(String username, String password, Model model, HttpServletRequest request,String startpage){

        //每页展示的数据
        int showpage = 10;

        System.out.println("startpage:　" + startpage);
        if (startpage == null){
            startpage = "0";
        }

        //当前页数
        int pagestart = Integer.valueOf(startpage);
        //此处pagestart不能为0
        if (pagestart == 0){
            pagestart = pagestart+1;
        }

        //登录
        User user = userService.login(username,password);

        //登录成功
        if (user != null){
            //登录成功查询出所有的file
            //List<Disk> allFile  = diskService.selectAll();
            int totalpage  = diskService.selectPage();
            List<Disk> allFile  = diskService.selectAllPage(pagestart,showpage,totalpage);
            System.out.println("总条数: " + totalpage);
            //封装数据
            Page page = new Page();
            page.setStartpage(pagestart);
            page.setTotalpage(totalpage);
            //如果每页展示的条数==总条数那么返回1页
            if (totalpage == showpage)
                page.setPagequantity(1);
            else if(totalpage%showpage == 0)
                page.setPagequantity(totalpage/showpage);
            else
                page.setPagequantity((totalpage/showpage) + 1);
            List<Type> allType = typeService.getAllType();
            model.addAttribute("disk",allFile);
            model.addAttribute("user",user);
            model.addAttribute("types",allType);
            model.addAttribute("page",page);
            //存入session
            request.getSession().setAttribute("user",user);



        }else
            model.addAttribute("message","用户名或密码错误");

        return user != null ? "disk" : "login";
    }



}
